package com.yyq.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yyq.eduservice.entity.EduChapter;
import com.yyq.eduservice.entity.EduCourse;
import com.yyq.eduservice.entity.EduVideo;
import com.yyq.eduservice.entity.EduVideoHistory;
import com.yyq.eduservice.service.EduChapterService;
import com.yyq.eduservice.service.EduCourseService;
import com.yyq.eduservice.service.EduVideoHistoryService;
import com.yyq.eduservice.service.EduVideoService;
import com.yyq.result.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yyq
 * @since 2021-05-20
 */
@CrossOrigin
@RestController
@RequestMapping("/eduservice/edu-video-history")
public class EduVideoHistoryController {
    @Autowired
    private EduVideoHistoryService videoHistoryService;

    @Autowired
    private EduCourseService courseService;

    @Autowired
    private EduVideoService videoService;

    @Autowired
    private EduChapterService chapterService;

    @PostMapping("addVideoHistory")
    public R addVideoHistory(@RequestBody EduVideoHistory videoHistory){
        //保存时间节点之前 确保 先删除之前该课程的时间节点
        QueryWrapper<EduVideoHistory> wrapper=new QueryWrapper<>();
        wrapper.eq("video_sourceId",videoHistory.getVideoSourceid());
        wrapper.eq("member_id",videoHistory.getMemberId());
        EduVideoHistory one = videoHistoryService.getOne(wrapper);

        //查封面
        if(one==null)  videoHistoryService.save(videoHistory);
        //否则先删除在添加
        else{
        videoHistoryService.remove(wrapper);
        videoHistoryService.save(videoHistory);
        }
        return R.ok();
    }

    @GetMapping("getVideoTime/{video_sourceId}/{member_id}")
    public R getVideoTime(@PathVariable String video_sourceId,@PathVariable String member_id){
        QueryWrapper<EduVideoHistory> wrapper=new QueryWrapper<>();
        wrapper.eq("video_sourceId",video_sourceId);
        wrapper.eq("member_id",member_id);
        EduVideoHistory one = videoHistoryService.getOne(wrapper);
        if(one==null) return R.ok().data("time",0);
        return R.ok().data("time",one.getTime());
    }

    @GetMapping("getFourHistory/{member_id}")
    public R getHistory(@PathVariable String member_id){
        QueryWrapper<EduVideoHistory> wrapper=new QueryWrapper<>();
        wrapper.eq("member_id",member_id);
        wrapper.orderByDesc("id");
        wrapper.last("limit 6");
        List<EduVideoHistory> list = videoHistoryService.list(wrapper);
        for (int i = 0; i < list.size(); i++) {
            //获取课程号
            QueryWrapper<EduVideo> wrapper1=new QueryWrapper<>();
            wrapper1.eq("video_source_id",list.get(i).getVideoSourceid());
            EduVideo one = videoService.getOne(wrapper1);

            list.get(i).setCourseId(one.getCourseId());
            list.get(i).setVideoTitle(one.getTitle());

            String ChapterID=one.getChapterId();

            QueryWrapper<EduChapter> wrapper3=new QueryWrapper<>();
            wrapper3.eq("id",ChapterID);
            EduChapter chapter = chapterService.getOne(wrapper3);
            list.get(i).setChapterTitle(chapter.getTitle());


            QueryWrapper<EduCourse> wrapper2=new QueryWrapper<>();
            wrapper2.eq("id",one.getCourseId());
            EduCourse course = courseService.getOne(wrapper2);

            list.get(i).setCourseCover(course.getCover());
            list.get(i).setCourseTitle(course.getTitle());
            //获取课程id

        }

//        //删除无用信息 保留最后一次观看的信息
//        int listLength=list.size();
//        for (int i = 0; i < listLength; i++){
//            EduVideoHistory history = list.get(i);
//            for (int  j=i+1;j<listLength;j++){
//                if (list.get(j).getCourseId().equals(history.getCourseId())){
//                    list.remove(j);
//                    listLength--;
//                }
//            }
//        }
        return R.ok().data("history",list);
    }


}

